#!/bin/bash

echo "======================================================"
echo "Neural-SLAM Enhancement Pipeline - Linux/Mac Setup"
echo "======================================================"

# Check if conda is available
if ! command -v conda &> /dev/null; then
    echo "ERROR: Conda is not available. Please install Anaconda/Miniconda first."
    exit 1
fi

echo ""
echo "📦 Setting up Conda environment..."
echo ""

# Create conda environment
conda create -n neural_slam_enhanced python=3.8 -y
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to create conda environment"
    exit 1
fi

echo ""
echo "⚡ Activating environment and installing PyTorch..."
echo ""

# Activate environment and install PyTorch
source $(conda info --base)/etc/profile.d/conda.sh
conda activate neural_slam_enhanced
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia -y
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to install PyTorch"
    exit 1
fi

echo ""
echo "📚 Installing additional requirements..."
echo ""

# Install additional requirements
pip install -r requirements_enhanced.txt
if [ $? -ne 0 ]; then
    echo "ERROR: Failed to install requirements"
    exit 1
fi

echo ""
echo "🧪 Testing environment setup..."
echo ""

# Test environment
python test_env.py
if [ $? -ne 0 ]; then
    echo "WARNING: Environment test had issues. Check the output above."
    echo "You may still be able to proceed depending on the specific errors."
    read -p "Press Enter to continue..."
fi

echo ""
echo "======================================================"
echo "Setup completed! Choose an option:"
echo "======================================================"
echo "1. Run complete pipeline (may take 12+ hours)"
echo "2. Run quick test pipeline (1-2 hours)"
echo "3. Run specific phase"
echo "4. Just generate paper results"
echo "5. Exit"
echo ""

read -p "Enter your choice (1-5): " choice

case $choice in
    1)
        echo ""
        echo "🚀 Starting complete pipeline..."
        python run_complete_pipeline.py
        ;;
    2)
        echo ""
        echo "⚡ Starting quick pipeline..."
        python run_complete_pipeline.py --quick-mode
        ;;
    3)
        echo ""
        echo "Available phases: 1, 2, 3, 4, 5, 6, 7"
        read -p "Enter phase number: " phase
        python run_complete_pipeline.py --phase $phase
        ;;
    4)
        echo ""
        echo "📄 Generating paper results..."
        python generate_paper_results.py
        ;;
    *)
        echo ""
        echo "👋 Exiting. Environment is ready for manual use."
        echo "To activate: conda activate neural_slam_enhanced"
        ;;
esac

echo ""
echo "======================================================"
echo "Pipeline execution completed!"
echo "======================================================"
echo "Check the generated files in:"
echo "- ./paper_results/ - LaTeX tables and figures"
echo "- ./experiments/ - Training logs and models"
echo "- ./logs/ - Detailed logs"
echo "======================================================"
